﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Collections.Generic;
using W.Purchase.Domain;
using W.Purchase.Read.API.AppLication.Command.FinanceTypeCommand;
using W.Purchase.Repository.Interface;

namespace W.Purchase.Read.API.Controllers
{
    /// <summary>
    /// 财务类别控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class FinanceTypeController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<FinanceTypeController> _logger;
        public FinanceTypeController(IMediator mediator, ILogger<FinanceTypeController> logger)
        {
            _mediator = mediator;
            _logger = logger;
        }
        /// <summary>
        /// 财务类别列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<FinanceType>>> GetFinanceTypeList([FromQuery]FinanceTypeShowCommand command)
        {
            ApiResult<List<FinanceType>> result = new ApiResult<List<FinanceType>>();
            try
            {
                _logger.LogInformation("调用财务类别列表");
                result.Data=await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用财务类别列表出错"+ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 财务类别反填
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<FinanceType>> GetFinanceTypeModel([FromQuery] FinanceTypeModelCommand command)
        {
            ApiResult<FinanceType> result = new ApiResult<FinanceType>();
            try
            {
                _logger.LogInformation("调用财务类别反填");
                result.Data = await _mediator.Send(command);
                return result;
            }
            catch (Exception ex)
            {
                _logger.LogError("调用财务类别反填出错" + ex.Message);
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
        }
        /// <summary>
        /// 导出财务类别
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> ExportExcel([FromQuery] FinanceTypeShowCommand command)
        {
            var list = await _mediator.Send(command);

            IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("财务类别信息");

            IRow rowl = sheet.CreateRow(0);
            rowl.CreateCell(0).SetCellValue("财务类别编号");
            rowl.CreateCell(1).SetCellValue("财务类别名称");
            rowl.CreateCell(2).SetCellValue("备注");


            int index = 1;
            foreach (var item in list)
            {
                rowl = sheet.CreateRow(index);
                rowl.CreateCell(0).SetCellValue(item.FinanceTypeNo);
                rowl.CreateCell(1).SetCellValue(item.FinanceTypeName);
                rowl.CreateCell(2).SetCellValue(item.FinanceTyperemark);
                index++;
            }
            byte[] bytes;

            using (MemoryStream ms = new MemoryStream())
            {
                book.Write(ms);
                bytes = ms.ToArray();
            }
            string mapPath = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(bytes, mapPath, "财务类别信息.xlsx");
        }
    }
}
